package com.yuyang.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.yuyang.springcloud.entities.CommonResult;
import com.yuyang.springcloud.entities.Payment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author yu yang
 * @date 2022/11/3 -22:46
 */
@RestController
public class RateLimitController
{
    @GetMapping("/byResource")
    @SentinelResource(value = "byResource",blockHandler = "handleException")
    public CommonResult byResource()
    {
        Payment payment = new Payment();
        payment.setId(2020L);
        return CommonResult.success(200,"按资源名称限流测试OK",payment);
    }
    public CommonResult handleException(BlockException exception)
    {
        return CommonResult.error(444,exception.getClass().getCanonicalName()+"\t 服务不可用");
    }
    @GetMapping("/rateLimit/byUrl")
    @SentinelResource(value = "byUrl")
    public CommonResult byUrl()
    {
        Payment payment = new Payment();
        payment.setId(2020L);
        return CommonResult.success(200,"按url限流测试OK",payment);
    }
}
